import { CreepsConfig } from '../../game/global'
import { LogHelper } from "../../utils/logHelper";

/**
 * 检查外矿房间是否需要申请建筑工
 */
export class outerOreBuildTask implements IOuterOreTask{
  run(room: Room, closestRoom: Room): void {

    // 如果有此房间的外矿建筑工则由外矿建筑工进行检查，否则旗子再进行检查
    const role = "build";

    if(!room){
      return;
    }

    // 查看房间内是否有未完成的工地列表
    if(!room.memory.constructions){
      room.memory.constructions = new Array<Id<ConstructionSite>>;
    }
    // 查看房间内是否有需要修复的建筑列表
    if(!room.memory.repairs){
      room.memory.repairs = new Array<Id<Structure>>;
    }

    // 检查房间内所有未完成的工地
    if(room.memory.constructions.length === 0){
      const constructions = room.find(FIND_MY_CONSTRUCTION_SITES);
      constructions.forEach(element => {
        if(room.memory.constructions.indexOf(element.id) === -1){
          room.memory.constructions.push(element.id);
        }
      });
    }
    // 检查房间内所有需要修复的建筑
    if(room.memory.repairs.length === 0){
      const structures = room.find(FIND_STRUCTURES, {
        filter: (structure) => {
          return (structure.structureType !== STRUCTURE_RAMPART) &&
                 (structure.hits < structure.hitsMax) &&
                 (structure.hits / structure.hitsMax < 0.4);
        }
      });
      structures.forEach(element => {
        if(room.memory.repairs.indexOf(element.id) === -1){
          room.memory.repairs.push(element.id);
        }
      });
    }

    if(room.memory.repairs.length > 0 || room.memory.constructions.length > 0){
      // 是否已经存在外矿建筑者
      const creeps = Object.values(Game.creeps).filter((creep) => 
        creep.memory.role === role && 
        creep.memory.outerRoom &&
        creep.memory.outerRoom === room.name);
      // 查找孵化队列中有没有外矿建筑者
      const queueCreeps = closestRoom.spawnQueueGetRole(role,(creeps) => creeps.memory.outerRoom !== undefined && creeps.memory.outerRoom === room.name);
      const spawn = closestRoom.spawn();
      
      const buildIndexCount = creeps.length + queueCreeps.length;
      const buildNeedCount = 1;

      if(spawn){
        
        if(buildIndexCount < buildNeedCount && !spawn.spawning){
          const controllerLevel = closestRoom.controller?.level;
          if(controllerLevel){
            const body = closestRoom.calculateCreepBody({
              [CARRY]:0.25,
              [MOVE]:0.25,
              [WORK]:0.5,
            },400);
            if(body){
              for (let index = 0; index < (buildNeedCount - buildIndexCount); index++) {
                closestRoom.spawnQueueAdd(role,body,{ role, bornRoom:closestRoom.name, outerRoom:room.name });
                LogHelper.log(`外矿房间管理[${room.name}] 申请孵化 ${role}`);
              }
            }
          }
        }
      }
    }
  }

  private getBody(level:number,role:string):BodyPartConstant[] | undefined{
    const controllerConfig = CreepsConfig.find((config:any) => config.controllerLevel === level);
    if(controllerConfig && controllerConfig.creeps.length > 0){
      const creep = controllerConfig.creeps.find((creep) => { return creep.role === role});
      if(creep){
        return creep.body;
      } 
      else{
        LogHelper.warn(`外矿房间管理[外矿建筑者}] 请注意，全局配置 等级 ${level} 的Creeps配置中没有 role ${role}`);
      }
    }
    else {
      LogHelper.warn(`外矿房间管理[外矿建筑者] 请注意，全局配置没有配置 等级 ${level} 的Creeps配置`);
    }
  }

}